package com.crm.common.exception;

import java.text.MessageFormat;

/**
 * @Description: 业务异常
 * @Author lixq-f
 * @Date 2021/5/14
 * @Version V1.0
 **/
public class BizBaseException extends RuntimeException {

    //异常code
    private Integer code;

    //异常消息
    private String message;

    //消息中配置的参数
    private Object[] arguments;


    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    @Override
    public String getMessage() {
        if (null != this.message && null != this.arguments && this.arguments.length > 0) {
            message = MessageFormat.format(message, this.arguments);
        }
        return message;
    }


    public BizBaseException(String message) {
        this(message,null,1, new Object[0]);
    }

    public BizBaseException(String message, Integer code) {
        this(message,null,code,new Object[0]);
    }

    public BizBaseException(String message,Integer code,Object ...arguments) {
        this(message,null,code, arguments);
    }


    public BizBaseException(String message, Throwable cause, Integer code) {
       this(message, cause, code, new Object[0]);
    }

    public BizBaseException(String message, Throwable cause, Integer code, Object ...arguments) {
        super(message, cause);
        this.code = code;
        this.message = message;
        this.arguments = arguments;
    }

    public BizBaseException(Exception e) {
        super(e);
    }

    /**
     * 重写该方法防止toString得到的消息为非国际化消息
     */
    @Override
    public String getLocalizedMessage() {
        return this.getMessage();
    }
}
